﻿//Copyright (c) 2010 William Liu, Enoch Ye
//William Liu <william.lyz@gmail.com>
//Enoch Ye <moonlight.enoch@gmail.com>

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GBG_DBM.Support.DAL.Entities;
using GBG_DBM.Support.DAL;
namespace GBG_DBM.UI
{
    public partial class RawMaterialSupplier_UI : Form
    {
        private Ingredient _Ingredient = new Ingredient();
        
        private DBAccessory _DBAccessory = new DBAccessory();
        private RawMaterialSupplier _RawMaterialSupplier = new RawMaterialSupplier();
        public RawMaterialSupplier_UI()
        {
            InitializeComponent();
        }
        public RawMaterialSupplier_UI(RawMaterialSupplier raw) 
        {
            InitializeComponent();
            _RawMaterialSupplier = raw;
            DataBind();
        }
        private void DataBind()
        {
            if (_RawMaterialSupplier == null)
            {
                txtSupplierCode.Text = _RawMaterialSupplier.Code;
                txtSupplierName.Text = _RawMaterialSupplier.Name;
                txtSupplierAddress.Text = _RawMaterialSupplier.Address;
                txtSupplierPhone.Text = _RawMaterialSupplier.Phone;
                txtSupplierEmail.Text = _RawMaterialSupplier.Email;
                txtContact.Text = _RawMaterialSupplier.Contact;
                txtCategory.Text = _RawMaterialSupplier.Category;
                rtbNotes.Text = _RawMaterialSupplier.Notes;
            }
            else{
                txtSupplierCode.Text = _RawMaterialSupplier.Code;
                txtSupplierCode.Enabled = false;
                txtSupplierName.Text = _RawMaterialSupplier.Name;
                txtSupplierName.Enabled = false;
                txtSupplierAddress.Text = _RawMaterialSupplier.Address;
                txtSupplierPhone.Text = _RawMaterialSupplier.Phone;
                txtSupplierEmail.Text = _RawMaterialSupplier.Email;
                txtContact.Text = _RawMaterialSupplier.Contact;
                txtCategory.Text = _RawMaterialSupplier.Category;
                rtbNotes.Text = _RawMaterialSupplier.Notes;
            }
        }

        private void btnSupplierOk_Click(object sender, EventArgs e)
        {
            if (_RawMaterialSupplier.Code==null)
            {
                this.En();
                _RawMaterialSupplier.Code = txtSupplierCode.Text;
                _RawMaterialSupplier.Name = txtSupplierName.Text;
                _RawMaterialSupplier.Phone = txtSupplierPhone.Text;
                _RawMaterialSupplier.Contact = txtContact.Text;
                _RawMaterialSupplier.Address = txtSupplierAddress.Text;
                _RawMaterialSupplier.Email = txtSupplierEmail.Text;
                _RawMaterialSupplier.Category = txtCategory.Text;
                _RawMaterialSupplier.Notes = rtbNotes.Text;
                _DBAccessory.addRawMaterialSupplier(_RawMaterialSupplier);
                AddIngredient_UI _AddIngredient = new AddIngredient_UI(_RawMaterialSupplier.Code,_RawMaterialSupplier.Name);
                _AddIngredient.Show();
            }
            else 
            {
                this.En();
                
                _RawMaterialSupplier.Name = txtSupplierName.Text;
                _RawMaterialSupplier.Phone = txtSupplierPhone.Text;
                _RawMaterialSupplier.Contact = txtContact.Text;
                _RawMaterialSupplier.Address = txtSupplierAddress.Text;
                _RawMaterialSupplier.Email = txtSupplierEmail.Text;
                _RawMaterialSupplier.Category = txtCategory.Text;
                _RawMaterialSupplier.Notes = rtbNotes.Text;
                _DBAccessory.setRawMaterialSupplier(_RawMaterialSupplier);
                this.Close();
            }
            
             //pass two specific parameter
             //AddIngredient_UI _AddIngredient = new AddIngredient_UI(_Ingredient);
             //_AddIngredient.Show();
            

        }
        private void En() 
        {
            Ok_RM_Supplier_UI.Enabled = false;
            Cancel_RM_Supplier_UI.Enabled = false;
        }

        private void Cancel_RM_Supplier_UI_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
